<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="所属校区">
        <campusSelect v-model="queryParams.campus" class="form-select" />
      </el-form-item>
      <el-form-item label="学员姓名">
        <el-input
          v-model="queryParams.signerName"
          placeholder="请输入学员姓名"
          clearable
        />
      </el-form-item>
      <el-form-item label="学员编号">
        <el-input
          v-model="queryParams.contractCode"
          placeholder="请输入学员编号"
          clearable
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"> </el-col>
    </el-row>

    <el-table v-loading="loading" :data="contractList">
      <el-table-column label="姓名" align="center" prop="signerName" />
      <el-table-column label="年级" align="center" prop="grade">
        <template #default="{ row }">
          <span>{{ formatGrade(row.grade) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户编号" align="center" prop="signerName" />
      <el-table-column label="名称" align="center" prop="signerName" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listContract,
  getContract,
  delContract,
  addContract,
  updateContract,
} from "@/api/system/contract";
import campusSelect from "@/components/Select/campusSelect.vue";

export default {
  name: "Contract",
  components: {
    campusSelect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractCode: null,
        note: null,
        week: null,
        signerId: null,
        isStudying: null,
        signerTime: null,
        status: null,
        principalApproveTime: null,
        cashierApproveTime: null,
        referrerId: null,
        isReferrer: null,
        isSustain: null,
        contractConfirmStatus: null,
        remainingClassTime: null,
        teacherId: null,
        cashierId: null,
        principalId: null,
        customerId: null,
        campusId: null,
        contractAttribute: null,
        contractType: null,
        contractMoney: null,
        payMoney: null,
        beginTime: null,
        remainingMoney: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      // 年级选项
      gradeOpiton: [],
    };
  },
  created() {
    this.gradeOpiton = [
      { value: 0, label: "小一" },
      { value: 1, label: "小二" },
      { value: 2, label: "小三" },
      { value: 3, label: "小四" },
      { value: 4, label: "小五" },
      { value: 5, label: "小六" },
      { value: 6, label: "初一" },
      { value: 7, label: "初二" },
      { value: 8, label: "初三" },
      { value: 9, label: "高一" },
      { value: 10, label: "高二" },
      { value: 11, label: "高三" },
      { value: 12, label: "高四" },
    ];

    this.getList();
  },
  methods: {
    /** 查询合同列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then((response) => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractCode: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        note: null,
        week: null,
        signerId: null,
        isStudying: null,
        signerTime: null,
        status: null,
        principalApproveTime: null,
        cashierApproveTime: null,
        referrerId: null,
        isReferrer: null,
        isSustain: null,
        contractConfirmStatus: null,
        remainingClassTime: null,
        teacherId: null,
        cashierId: null,
        principalId: null,
        customerId: null,
        campusId: null,
        contractAttribute: null,
        contractType: null,
        contractMoney: null,
        payMoney: null,
        beginTime: null,
        remainingMoney: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同";
    },
    /** 修改按钮操作 */
    handleDetail({ customerId }) {
      this.$router.push({
        path: "/contract/detail",
        query: { id: customerId },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除合同编号为"' + ids + '"的数据项？')
        .then(function () {
          return delContract(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/contract/export",
        {
          ...this.queryParams,
        },
        `contract_${new Date().getTime()}.xlsx`
      );
    },

    formatGrade(grade) {
      return _.find(this.gradeOpiton, { value: grade })?.label || "未知年级";
    },
  },
};
</script>
